package com.code.app.db;

import android.database.sqlite.SQLiteDatabase;
import android.os.Environment;

import java.io.File;

public class DaoSupportFactory {

    private static DaoSupportFactory daoSupportFactory;

    private SQLiteDatabase sqLiteDatabase;

    private DaoSupportFactory() {
        File rootDir = new File(Environment.getExternalStorageDirectory().getAbsolutePath()
                + File.separator + "mirror"
                + File.separator + "database" + File.separator);
        if (!rootDir.exists()) {
            rootDir.mkdirs();
        }
        File dbFile = new File(rootDir, "mirror.db");
        //打开或者创建一个数据库
        sqLiteDatabase = SQLiteDatabase.openOrCreateDatabase(dbFile, null);
    }

    public static DaoSupportFactory getInstance() {
        if (daoSupportFactory == null) {
            synchronized (DaoSupportFactory.class) {
                if (daoSupportFactory == null) {
                    daoSupportFactory = new DaoSupportFactory();
                }
            }
        }
        return daoSupportFactory;
    }

    public <T> IDaoSupport<T> getDao(Class<T> cls) {
        DaoSupport<T> daoSupport = new DaoSupport<>();
        daoSupport.init(this.sqLiteDatabase, cls);
        return daoSupport;
    }
}
